package karaoke.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Administrador;
import karaoke.modelo.JsonResult;
import karaoke.modelo.Local;
import karaoke.negocio.GestionAdministradores;
import karaoke.negocio.GestionLocales;

/**
 * Servlet implementation class LoginUsuarioServlet
 */
@WebServlet("/admin/LoginAdminServlet")
public class LoginAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginAdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String dni = request.getParameter("txtdni");
		String password = request.getParameter("txtpassword");
		
		GestionAdministradores negocioAdm = new GestionAdministradores();
		JsonResult resultado = new JsonResult();
		RequestDispatcher rd = null;
		
		try {
			Administrador adm = negocioAdm.logear(dni, password);
			
			if(adm.getIdadministrador()>0){
				HttpSession sesion = request.getSession();
				sesion.setAttribute("ADMINISTRADOR", adm);
				String tipo = adm.getTipo();
				
				switch (tipo) {
					case "S": {
						rd = request.getRequestDispatcher("/admin/Locales.jsp");
						break;
					}
					case "L": {
						rd = request.getRequestDispatcher("/admin/productos.jsp");
						GestionLocales negocioL = new GestionLocales();
						Local l = negocioL.obtenerByIdAdmin(adm.getIdadministrador());
						sesion.setAttribute("LOCAL", l);
						break;
					}
					case "C": {
						rd = request.getRequestDispatcher("/comprobante_pago.jsp");
						GestionLocales negocioL = new GestionLocales();
						Local l = negocioL.obtenerByIdAdmin(adm.getIdadministrador());
						sesion.setAttribute("LOCAL", l);
						break;
					}
					default:
						rd = request.getRequestDispatcher("/admin/index.jsp");
						break;
				}
				
			} else {
				resultado.addMenssage(JsonResult.WARNING ,"Ingreso Invalido");
				rd = request.getRequestDispatcher("/admin/index.jsp");
			}
			
		} catch (DAOExcepcion e) {
			resultado.addMenssage(JsonResult.ERROR,"Fallo el ingreso");
		}
		
		request.setAttribute("MESSAGES", resultado);
		rd.forward(request, response);
	}
}
